/*
 * @lc app=leetcode.cn id=167 lang=typescript
 *
 * [167] 两数之和 II - 输入有序数组
 */

// @lc code=start

//  思想：双指针(因为数组有序)

function twoSum(numbers: number[], target: number): number[] {
    let left = 0, right = numbers.length - 1
    while (left < right) {
        const sum = numbers[left] + numbers[right]
        // 题目索引从 1 开始
        if (sum === target) {
            return [left + 1, right + 1]
        } else if (sum < target) {
            left++
        } else {
            right--
        }
    }
    return [-1, -1]
};
// @lc code=end

console.log(twoSum([2, 7, 11, 15], 9))  // [1,2]
console.log(twoSum([2, 3, 4], 6))       // [1,3]
console.log(twoSum([-1, 0], -1))        // [1,2]
